package cn.byapps.attendance.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.byapps.attendance.dao.AdDao;
import cn.byapps.attendance.entity.Ad;
import cn.byapps.attendance.service.AdService;
import cn.byapps.attendance.util.DataTablesResult;
import core.service.impl.BaseServiceImpl;

@Service
public class AdServiceImpl extends BaseServiceImpl<Ad> implements AdService {

	@Resource
	private AdDao adDao;

	@Override
	public Ad add(Ad entity) {
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("md5", entity.getMd5());
		List<Ad> ads = adDao.findAll(maps);
		for (Ad ad : ads) {
			return ad;
		}
		super.save(entity);
		return entity;
	}

	@Override
	public List<Ad> listAll() {
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("flag",true);
		return adDao.findAll(maps);
	}

	@Override
	public DataTablesResult<Ad> getData(Integer draw,Integer pos,Integer rows) {
		DataTablesResult<Ad> ads = new DataTablesResult<Ad>();
		ads.setDraw(draw);
		ads.setData(adDao.find(pos, rows));
		long total = adDao.count();
		ads.setRecordsFiltered(total);
		ads.setRecordsTotal(total);
		return ads;
	}

	@Override
	public Ad updateName(Ad entity) {
		Ad ad  = adDao.get(entity.getId());
		ad.setName(entity.getName());
		return ad;
	}

	@Override
	public Ad updateFlag(String id) {
		Ad ad  = adDao.get(id);
		ad.setFlag(!ad.getFlag());
		return ad;
	}

}
